import os
import shutil
import sys
import time
from optparse import OptionParser

def customize_effect_project(project_name, vst_sdk_dir, version, author, vendor, description):
    os.rename("XXX.h", project_name.lower()+".h")
    with open(project_name.lower()+".h", "r") as header:
        headerText = header.read()
    headerText = headerText.replace("<DATE>", time.strftime("%d %b %Y - %X", time.localtime()))
    headerText = headerText.replace("<project_name>", project_name.lower())
    headerText = headerText.replace("<Project_name>", project_name.capitalize())
    headerText = headerText.replace("<description>", description)
    headerText = headerText.replace("<author>", author)
    with open(project_name.lower()+".h", "w") as header:
        header.write(headerText)
    print "header file generated"
    
    #source
    os.rename("XXX.cpp", project_name.lower()+".cpp")
    with open(project_name.lower()+".cpp", "r") as source:
        sourceText = source.read()
    sourceText = sourceText.replace("<DATE>", time.strftime("%d %b %Y - %X", time.localtime()))
    sourceText = sourceText.replace("<project_name>", project_name.lower())
    sourceText = sourceText.replace("<Project_name>", project_name.capitalize())
    sourceText = sourceText.replace("<version>", version)
    sourceText = sourceText.replace("<author>", author)
    sourceText = sourceText.replace("<vendor>", vendor)
    sourceText = sourceText.replace("<description>", description)
    with open(project_name.lower()+".cpp", "w") as source:
        source.write(sourceText)
    print "source file generated"
    
    #scons
    with open("SConstruct", "r") as sconstruct:
        sconstructText=sconstruct.read()
    sconstructText = sconstructText.replace("<VST_SDK_DIR>", vst_sdk_dir)
    sconstructText = sconstructText.replace("<project_name>", project_name.lower())
    with open("SConstruct", "w") as sconstruct:
        sconstruct.write(sconstructText)
    print "scons file generated"
    

if __name__=="__main__":
    usage = "usage: %prog [-t {VSTi, VSTfx}] project_name"
    parser = OptionParser(usage=usage)
    parser.add_option("-t", "--project-type", dest="project_type", default="VSTfx",
                      help="select between 'VSTi' and 'VSTfx' for vst instrument and vst effect respectively")
    parser.add_option("-s", "--sdk-dir", dest="vst_sdk_dir", default="C:/code/vst/vstsdk2.4",
                      help="sets the dir where the vst sdk is located, to generate the scons build file")
    parser.add_option("-v", "--version", dest="version", default="1000",
                      help="sets the version of the project")
    parser.add_option("-a", "--author", dest="author", default="",
                      help="sets the author of the project")
    parser.add_option("-b", "--vendor", dest="vendor", default="",
                      help="sets the vendor of the project")
    parser.add_option("-d", "--description", dest="description", default="",
                      help="sets the description of the project")
    
    #sys.argv.append("-v 1001")
    #sys.argv.append("-a Author_name")
    #sys.argv.append("Test")
    (options, args) = parser.parse_args()
    print "options:",options
    print "args:",args
    
    if len(args)!=1:
        print "incorrect number of arguments"
        parser.print_help()
        sys.exit()
    
    project_name = args[0]
    
    try:shutil.rmtree(project_name.lower())
    except:pass
    shutil.copytree("template_effect", project_name.lower())
    os.chdir(project_name.lower())
    os.chdir("source")
    
    if options.project_type.lower()=="vsti":
        print "vsti not yet implemented"
        sys.exit()
    elif options.project_type.lower()=="vstfx":
        print "generating vst effect project"
        customize_effect_project(project_name, options.vst_sdk_dir,
                                 options.version, options.author,
                                 options.vendor, options.description)